package com.singzi.leetcode.N500;

public class T0453 {
    public static void main(String[] args) {
        int []nums = new int[3];
//        for (int i = 0; i < nums.length; i++) {
//            nums[i] = i+1;
//            int a =  Math.abs(nums[i]);
//        }
        nums[0] = 1;
        nums[1] = 1;
        nums[2] =1000000000;
        Solution0453 solution0453 = new Solution0453();
        System.out.println(solution0453.minMoves(nums));

    }
}
class Solution0453 {
    private int max_index;
    private int max;
    private int max2;
    public int minMoves(int[] nums) {
        if(eaqulNums(nums)) return 0;
        int count = 0;
        count +=getGap(nums);
        while(true){
            if(eaqulNums(nums)){
                break;
            }

            count +=1;
            addAnother(nums);
        }
        return count;
    }

    public int getGap(int[]nums){
        if(nums[0] < nums[1]) {
            max = nums[0];
            max2 = nums[1];
            max_index = 1;
        }
        for(int i = 2;i < nums.length;i++){
            if(nums[i]>max){
                max2 = max;
                max = nums[i];
                max_index = i;
            }
            else if(nums[i]>max2){
                max2 = nums[i];
            }
        }
        int gap = Math.abs(max - max2) - 1;
        for(int i = 0;i < nums.length;i++){
            if(i == max_index) continue;
            nums[i] +=gap;
        }
        return gap;


    }

    public void addAnother(int[] nums){

        getIndexMax(nums);
        for(int i = 0;i < nums.length;i++){
            if(i == max_index) continue;
            nums[i] +=1;
        }

    }
    public void getIndexMax(int[] nums){
        max = nums[0];
        max_index = 0;

        for(int i = 1;i < nums.length;i++){
            if(nums[i] > max){
                max = nums[i];
                max_index = i;
            }
        }
    }
    public boolean eaqulNums(int[] nums){
        if(nums.length==1||nums.length==0) return true;
        boolean flag = true;
        for(int i = 1;i < nums.length;i++){
            if(nums[i]!=nums[0]){
                flag = false;
                break;
            }
        }
        return flag;
    }
}